package com.aven.blog.dao.impl;

import com.aven.blog.bean.User;
import com.aven.blog.dao.IUserDao;
import com.aven.blog.utils.HibernateUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

public class IUserDaoImpl implements IUserDao {
    Session session = HibernateUtils.openSession();//开启session,自动关闭

    @Override
    public User findByUsername(String name) {  //按用户名查找用户
        session.beginTransaction();     //开启事务，自动关闭
        System.out.println(name);
        Query query = session.createQuery("from User u where u.username=:n");
        query.setParameter("n", name);
        System.out.println("查找用户" + query.uniqueResult());
        return (User) query.uniqueResult();
    }

    @Override
    public List<User> findByUsernameForResults(String name) {
        return null;
    }

    @Override
    public int addUser(User user) {
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        return 1;

    }
}
